<template>
	<view>
		<view>
			<!-- 顶部 -->
			<view class="top-view">
				<view class="top-left">
					<image src="../../images/phone-icon.png"></image>
					<text>iphone X</text>
					<text class="separate">|</text>
					<view class="edit">
						<text>修改</text>
						<image src="../../images/next.png"></image>
					</view>
				</view>
				<view class="top-right" @click="onExport">
					<text>下一步</text>
					<text>选择规格</text>
				</view>
			</view>

			<!-- 左侧选项栏 -->
			<view class="left-tab">
				<view v-for="(item, index) in leftList" :key="index" class="list_item2" :class="activeIndex2 == index ? 'on3' : ''" @click="tabClick2" :data-index="index">
					{{ item }}
				</view>
			</view>

			<view class="canvas-view">
				<canvas-drag-component bgColor="" :bgImage="bgImg" bgSourceId="" :graph="graph" :width='600' :height='790'></canvas-drag-component>
				<image :src="camera" class="camera" :style="[{ width: cameraW + 'px', height: cameraH + 'px' }]"></image>
			</view>

			<!-- 内容 -->
			<view class="content">
				<view v-if="activeIndex == 0">
					<scroll-view class="scroll-view_H" scroll-x style="width: 94%">
						<view class="scroll-view-item" v-for="(item, index) in addImgList">
							<image :src="item.url" class="imgItem" @longtap="longTap"></image>
							<image src="../../images/x.png" class="del" :data-index="index" @click="delImg"></image>
						</view>
					</scroll-view>
				</view>
				<view v-if="activeIndex == 1">
					<scroll-view class="scroll-view_H" scroll-x style="width: 94%">
						<view class="scroll-view-item"><image src="../../images/upImg.png" class="add" @click="showModal"></image></view>
						<view class="scroll-view-item" v-for="(item, index) in addImgList">
							<image :src="item.url" class="imgItem"></image>
							<image src="../../images/x.png" class="del" :data-index="index" @click="delImg"></image>
						</view>
					</scroll-view>
				</view>
				<view v-if="activeIndex == 3">
					<scroll-view class="scroll-view_H2" scroll-x style="width: 94%">
						<view class="font-item" v-for="(item, index) in fontList">
							<text :style="[{ fontFamily: item.fontFamily }]" :data-font="item.fontFamily" @click="changeFont">{{ item.text }}</text>
						</view>
					</scroll-view>
					<view class="textIn">
						<input type="text" placeholder="请输入文字" @blur="getText" />
						<button mini @click="onAddText">添加</button>
					</view>
				</view>
			</view>

			<!-- 选项栏 -->
			<view class="title">
				<view v-for="(item, index) in list" :key="index" class="list_item" :class="activeIndex == index ? 'on2' : ''" @click="tabClick" :data-index="index">
					{{ item }}
				</view>
			</view>

			<!-- 弹框 -->

			<view class="modals modals-bottom-dialog" :hidden="hideModal">
				<view class="modals-cancel" @click="hideModal2"></view>
				<view class="bottom-dialog-body bottom-pos" :animation="animationData">
					<view @click="onAddImage" class="m-li">上传图片</view>
					<view class="m-li" @click="toList">选择模板</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import CanvasDragComponent from '@/components/canvas-drag';
import CanvasDrag from '@/util/canvas-drag';

export default {
	components: {
		CanvasDragComponent
	},
	data() {
		return {
			graph: {},
			width: '',
			height: '',
			camera: '../../images/shexiang.png',
			top: 20,
			left: 240,
			cameraW: 80,
			cameraH: 200,
			addImgList: [],
			hideModal: true, //模态框的状态  true-隐藏  false-显示
			animationData: {}, //
			delIndex: '',
			leftList: ['选项1', '选项2'],
			list: ['选择图层', '上传图片', '选择素材', '添加文字'],
			activeIndex: 0,
			activeIndex2: 0,
			text: '',
			canvasList: [],
			bgImg: '../../images/phone.png',
			fontList: [
				{
					text: '字体1',
					fontFamily: 'Courier'
				},
				{
					text: '字体2',
					fontFamily: 'Tahoma'
				},
				{
					text: '字体3',
					fontFamily: 'inherit'
				},
				{
					text: '字体4',
					fontFamily: 'Cambria'
				}
			],
			colorList: [{}]
		};
	},
	created(options) {
		
		uni.setStorageSync('canvasList', '');
		// 页面渲染完成
		var _this = this;
		uni.getImageInfo({
			src: '../../images/phone.png',
			success: function(res) {
				// console.log(res)
				if (options.imgUrl) {
					uni.getImageInfo({
						src: options.imgUrl,
						success(res2) {
							// console.log(res2)
							let height = res2.height;
							let width = res2.width;
							let json = {};
							json.url = options.imgUrl;
							_this.addImgList.unshift(json);
							// console.log(addImgList)

							(_this.width = res.width * 2), (_this.height = res.height * 2), (_this.graph.w = width);
							_this.graph.h = height;
							_this.graph.type = 'image';
							_this.graph.url = options.imgUrl;

							_this.addImgList = _this.addImgList;
						}
					});
				}
			}
		});
	},
	mounted() {
		CanvasDrag.changeBgImage(this.bgImg);
	},
	methods: {
		/**
		 * 添加图片
		 */
		onAddImage() {
			let _this = this;
			uni.chooseImage({
				success: res => {
					uni.getImageInfo({
						src: res.tempFilePaths[0],
						success(res2) {
							let height = res2.height;
							let width = res2.width;
							// _this.setData({
							//   graph: {
							//     w: width,
							//     h: height,
							//     type: 'image',
							//     url: res.tempFilePaths[0],
							//   }
							// });
							_this.graph.w = width;
							_this.graph.h = height;
							_this.graph.type = 'image';
							_this.graph.url = res.tempFilePaths[0];

							CanvasDrag.exportJson()
								.then(imgArr => {
									let addImgList = [];
									for (let item of imgArr) {
										if (item.type === 'image') {
											addImgList.unshift(item);
										}
									}
									// _this.setData({
									//   addImgList: addImgList,
									// })
									_this.addImgList = addImgList;
								})
								.catch(e => {
									console.error(e);
								});
						}
					});
				}
			});
			_this.hideModal();
		},
		/**
		 * 导出图片
		 */
		onExport() {
			CanvasDrag.export()
				.then(filePath => {
					console.log(filePath);
					uni.previewImage({
						urls: [filePath]
					});
				})
				.catch(e => {
					console.error(e);
				});

			CanvasDrag.exportJson()
				.then(imgArr => {
					console.log(imgArr);
				})
				.catch(e => {
					console.error(e);
				});
		},
		getText(e) {
			let _this = this;
			// _this.setData({
			//   text: e.detail.value
			// })
			_this.text = e.detail.value;
		},
		onAddText() {
			let text = this.text;
			this.graph.type = 'text';
			this.graph.text = text;
			// this.setData({
			//   graph: {
			//     type: 'text',
			//     text: text,
			//   }
			// });
		},
		// delImg(e) {
		//   var that = this;
		//   //获取下标
		//   var idx = e.target.dataset.index;
		//   that.addImgList.splice(idx, 1)
		//   CanvasDrag.delItem(idx)
		//   // console.log(that.data.addImgList)
		//   // that.setData({
		//   //   addImgList: that.data.addImgList,
		//   // })
		//   that.addImgList= that.addImgList,

		// },
		changeFont(e) {
			const font = e.target.dataset.font;
			CanvasDrag.changFont(font);
		},
		tabClick(e) {
			var that = this;
			var idx = e.target.dataset.index;
			// that.setData({
			//   activeIndex: idx
			// })
			that.activeIndex = idx;
		},
		tabClick2(e) {
			var that = this;
			var idx = e.target.dataset.index;
			CanvasDrag.exportJson()
				.then(imgArr => {
					let json = {};
					json.id = that.activeIndex2;
					imgArr.unshift(json);
					let canvasList = that.data.canvasList;
					if (canvasList != []) {
						if (canvasList.findIndex(element => element[0].id === that.data.activeIndex2)) {
							canvasList.splice(that.activeIndex2, 1, imgArr);
						}
					} else {
						canvasList.push(imgArr);
					}
					console.log(canvasList);
					uni.setStorageSync('canvasList', '');

					// that.setData({
					//   activeIndex2: idx
					// })
				})
				.catch(e => {
					console.error(e);
				});
		},
		// 显示遮罩层
		showModal: function() {
			var that = this;
			// that.setData({
			//   hideModal: false
			// })
			that.hideModal = false;

			var animation = uni.createAnimation({
				duration: 600, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
				timingFunction: 'ease' //动画的效果 默认值是linear
			});
			this.animation = animation;
			setTimeout(function() {
				that.fadeIn(); //调用显示动画
			}, 200);
		},

		// 隐藏遮罩层
		hideModal2: function() {
			var that = this;
			var animation = wx.createAnimation({
				duration: 800, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
				timingFunction: 'ease' //动画的效果 默认值是linear
			});
			this.animation = animation;
			that.fadeDown(); //调用隐藏动画
			setTimeout(function() {
				// that.setData({
				//   hideModal: true
				// })
				that.hideModal = true;
			}, 720); //先执行下滑动画，再隐藏模块
		},

		//动画集
		fadeIn: function() {
			this.animation.translateY(0).step();
			this.animationData = this.animation.export();
			// this.setData({
			//   animationData: this.animation.export() //动画实例的export方法导出动画数据传递给组件的animation属性
			// })
		},
		fadeDown: function() {
			this.animation.translateY(300).step();
			this.animationData = this.animation.export();
		},
		toList: function() {
			this.hideModal2();
			uni.navigateTo({
				url: '../ordinaryPreview/ordinaryPreview'
			});
		},
		longTap() {
			console.log('长按');
		}
	}
};
</script>

<style>
.top-view {
	display: flex;
	justify-content: space-between;
}

.top-left {
	display: flex;
	align-items: center;
}

.top-left image {
	height: 40rpx;
	width: 35rpx;
	margin: 0 10rpx;
}

.separate {
	margin: 0 16rpx;
	color: #c5c5c5;
}

.edit {
	display: flex;
	align-items: center;
}

.edit text {
	color: #1196db;
	font-size: 26rpx;
}

.edit image {
	height: 18rpx;
	width: 22rpx;
}

.top-right {
	display: flex;
	flex-direction: column;
	background-color: #ec6120;
	text-align: center;
	padding: 7rpx 70rpx;
}

.top-right text {
	font-size: 24rpx;
	color: #fff;
	/* margin: 1rpx; */
}

.contentWarp {
	position: absolute;
	width: 100%;
	height: 100%;
	top: 200rpx;
	left: 0;
	bottom: 0;
	right: 0;
	margin: auto;
	background-color: #efefef;
	padding-top: 40rpx;
}

.touchWrap {
	transform-origin: center;
	position: absolute;
	z-index: 100;
}

.imgWrap {
	box-sizing: border-box;
	width: 100%;
	transform-origin: center;
	float: left;
	border: 5rpx transparent dashed;
}

.imgWrap image {
	float: left;
}

.touchActive .x {
	display: block;
}

.touchActive .o {
	display: block;
}

.x {
	position: absolute;
	top: -25rpx;
	left: -25rpx;
	z-index: 500;
	display: none;
	width: 50rpx;
	height: 50rpx;
	overflow: hidden;
	font-weight: bold;
	color: #efefef;
}

.o {
	position: absolute;
	bottom: -25rpx;
	right: -25rpx;
	width: 50rpx;
	height: 50rpx;
	text-align: center;
	display: none;
	overflow: hidden;
	font-weight: bold;
	color: #efefef;
}

.active {
	background-color: rgb(78, 114, 151);
}

.active view {
	border: none;
}

.touchActive {
	/* border: 4rpx #fff dashed; */
	z-index: 400;
}

.swiper-tab {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	width: 100%;
	border-bottom: 2rpx solid #d0d0d0;
	text-align: center;
	line-height: 80rpx;
}

.swiper-tab-list {
	font-size: 30rpx;
	display: inline-block;
	width: 50%;
	color: #272727;
}

.on {
	color: #f16d30;
}

.swiper-box {
	display: block;
	height: 100%;
	width: 100%;
	overflow: hidden;
}

.swiper-box view {
	text-align: center;
}

.canvas-view {
	padding-top: 40rpx;
	width: 375rpx;
	margin: 0 auto;
	text-align: center;
	position: relative;
}

.camera {
	position: absolute;
	top: 80rpx;
	left: 50rpx;
	width: 55rpx;
	height: 120rpx;
	z-index: 999;
}

.scroll-view_H {
	position: fixed;
	bottom: 120rpx;
	background-color: #fff;
	display: flex;
	white-space: nowrap;
	margin: 0 10rpx;
	padding: 10rpx;
}

.scroll-view_H2 {
	display: flex;
	white-space: nowrap;
	margin: 0 10rpx;
	padding: 10rpx;
}

.scroll-view-item {
	display: inline-block;
	/* background-color: #fff; */
	margin: 0 10rpx;
	position: relative;
	padding-top: 18rpx;
}

.scroll-view-item .imgItem {
	width: 120rpx;
	height: 120rpx;
	border-radius: 5px;
}

.scroll-view-item .add {
	width: 120rpx;
	height: 120rpx;
	border-radius: 5px;
}

.del {
	position: absolute;
	right: -8rpx;
	top: 2rpx;
	height: 30rpx;
	width: 30rpx;
}

.container {
	height: 100%;
	min-height: 100%;
	display: flex;
	flex-direction: column;
	box-sizing: border-box;
}

.title {
	width: 100%;
	height: 88rpx;
	background: white;
	display: flex;
	align-items: center;
	justify-content: space-around;
	z-index: 999;
}

.titleSel {
	color: #5f6fee;
	font-size: 32rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
}

.titleUnsel {
	color: #858fab;
	font-size: #858fab;
}

.headerLineSel {
	background: #5f6fee;
	height: 6rpx;
	width: 40rpx;
	position: relative;
	margin-top: 10rpx;
}

.headerLineUnsel {
	background: #fff;
	height: 6rpx;
	width: 40rpx;
	position: relative;
	margin-top: 10rpx;
}

.swiper {
	width: 100%;
	flex: 1;
	height: 100vh;
	overflow: scroll;
}

.recordItem {
	margin-top: 10rpx;
	background-color: white;
	padding-bottom: 20rpx;
	padding-top: 20rpx;
}

.title {
	display: flex;
	flex-direction: row;
	position: fixed;
	bottom: 10rpx;
	left: 0;
	right: 0;
}

.list_item {
	flex-grow: 1;
	box-sizing: border-box;
	height: 85rpx;
	font-size: 32rpx;
	text-align: center;
	line-height: 85rpx;
}

.on2 {
	color: #f16d30;
}

.textIn {
	display: flex;
	justify-content: center;
	align-items: center;
}

.textIn input {
	border: 1px solid #efefef;
	border-radius: 10rpx;
	font-size: 30rpx;
	padding: 8rpx 4rpx;
}

.textIn button {
	margin: 4rpx;
	font-size: 28rpx;
	text-align: center;
	background-color: #f16d30;
	color: #fff;
}

.font-item {
	display: inline-block;
	margin: 8rpx;
}

/*模态框*/

.modals {
	position: fixed;
	z-index: 999;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
}

.modals-cancel {
	position: absolute;
	z-index: 1000;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background-color: rgba(0, 0, 0, 0.5);
}

.bottom-dialog-body {
	position: absolute;
	z-index: 10001;
	bottom: 0;
	left: 0;
	right: 0;
	padding: 30rpx;
	height: 300rpx;
	background-color: #fff;
	display: flex;
	justify-content: space-around;
}
.m-li {
	margin-top: 100upx;
	width: 240upx;
}

/*动画前初始位置*/

.bottom-pos {
	-webkit-transform: translateY(100%);
	transform: translateY(100%);
}

/* 左侧 */
.left-tab {
	position: fixed;
	left: 20rpx;
	top: 150rpx;
}
.list_item2 {
	border: 1px solid #bbbbbb;
	border-radius: 10rpx;
	text-align: center;
	height: 60rpx;
	line-height: 60rpx;
	width: 160rpx;
	margin: 16rpx 0;
}
.on3 {
	color: #ec6120;
	border: 1px solid #ec6120;
}
</style>
